package com.audi.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

import com.audi.R;
import com.audi.BaseActivity;
import com.audi.adapter.SmartTriggerAdapter;
import com.audi.dto.SmartTriggerItem;
import com.audi.message.ActionMessage;
import com.audi.message.SendMessage;

@SuppressLint("HandlerLeak")
public class SmartTriggerActivity extends BaseActivity {

	private SmartTriggerAdapter mSmartTriggerAdapter;
	private ArrayList<SmartTriggerItem> mListTriggerArr;
	private ImageView mSmartTriggerDrawer;
	ListView listTrigger;
	Activity mActivity;
	SendMessage sendMessageSmartTrigger = new SendMessage();

	protected void onCreate(Bundle bundle) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(bundle);
		setContentView(R.layout.activity_smart_trigger_page);
		mActivity = this;
		initActionBar();
		setListenerActionSmartTrigger();
		initView();
		initListener();

	}

	private void initView() {
		listTrigger = (ListView) findViewById(R.id.list_trigger);
		intData();
	}

	private void initListener() {
		// Code listener here
	}

	/*
	 * add data to list of triggers
	 */
	private void intData() {

	}

	protected void initActionBar() {
		mSmartTriggerDrawer = (ImageView) findViewById(R.id.btn_devices_name_drawer);
		mSmartTriggerDrawer.setImageResource(R.drawable.action_menu_more);
		ImageButton newtrig = (ImageButton) findViewById(R.id.btn_menu_devices_card);
		newtrig.setImageResource(R.drawable.action_add);
		newtrig.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						AddActionCardActivity.class);
				intent.putExtra("TYPE_ACTION", 0);
				startActivity(intent);

			}
		});
		mSmartTriggerDrawer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View e) {
				if (!mDrawerLayout.isDrawerOpen(mDrawerList)) {
					mDrawerLayout.openDrawer(mDrawerList);
				} else {
					mDrawerLayout.closeDrawer(mDrawerList);
				}
			}
		});

	}

	@Override
	public void onRefreshData() {
		// TODO Auto-generated method stub

	}

	public void setListenerActionSmartTrigger() {
		txtHomeAction.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!mDrawerLayout.isDrawerOpen(mDrawerList)) {
					mDrawerLayout.openDrawer(mDrawerList);
				} else {
					mDrawerLayout.closeDrawer(mDrawerList);
				}
			}
		});
	}

	Handler handlerSmartTrigger = new Handler() {

		public void handleMessage(final Message msg) {
			runOnUiThread(new Runnable() {
				public void run() {
					Log.d("LOG=========", "===== get list TRIGGER");
					try {
						if (msg.arg1 != -1) {
							mListTriggerArr = new ArrayList<SmartTriggerItem>();
							ArrayList<SmartTriggerItem> result = (ArrayList<SmartTriggerItem>) msg
									.getData().getSerializable("3013");

							mListTriggerArr = result;
							loadDataSmartTriggerItem(mListTriggerArr);
							Log.d("LOG=========", "===============>>>>>>>");
							Log.d("LOG=========", "marboro====="
									+ mListTriggerArr.size());
							Log.d("LOG=========", "===============>>>>>>>");
							for (int i = 0; i < result.size(); i++) {
								Log.d("LOG=========", "::::::>>>>>>> "+
							result.get(i).getWhen().getWhenId());
							}
						}
					} catch (Exception e) {
						Log.e("TEST", "[TUTORIAL]:" + e.toString());
					}
				}
			});
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		sendMessageSmartTrigger.sendData(ActionMessage.GET_ALL_ST, null,
				handlerSmartTrigger);
	}

	public void loadDataSmartTriggerItem(
			ArrayList<SmartTriggerItem> resultSmartTriggerItem) {
		mListTriggerArr = resultSmartTriggerItem;
		if (mListTriggerArr != null && mListTriggerArr.size() > 0) {
			if (mSmartTriggerAdapter == null) {
				mSmartTriggerAdapter = new SmartTriggerAdapter(this,
						mListTriggerArr);
				listTrigger.setAdapter(mSmartTriggerAdapter);
			} else {
				mSmartTriggerAdapter.swapItems(mListTriggerArr);
			}
		} else {
			Log.e("--NULLL-----", "------------NULLL--------------");
		}
	}
}
